//
//  OADAlertViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 5/10/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADAlertViewController.h"
#import "OAAlertManager.h"
#import "CDXRootViewController.h"
#import "OADAlertContentViewController.h"

@interface OADAlertViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OADAlertViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"class": UITableViewCell.class,
          @"title": @"One",
          @"target": UIViewController.class,
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Two",
          @"target": @"UIViewController",
        },
        @{@"title": @"Three",
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Four",
          @"target": UIViewController.class,
        },
    ];
    
    [self.cells addObjectsFromArray:cells];
}

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class cellClass = cellInfo[@"class"];
    return cellClass ?: UITableViewCell.class;
}

- (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    return cellInfo[@"title"];
}

- (UIViewController *)targetViewControllerAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    return targetVc;
}

- (void)showAlertView {
//    NSLog(@"111");
    
    [self showAlertViewNew];
//    [self showAlertViewOld];
    
//    [self customAlertTitle:@"Title" message:@"message"];
}



#define AlertControllerTitleKey 0
#define AlertControllerStyleKey 1
#define AlertControllerHandlerKey 2

- (void)doIt {
    CDXRootViewController *vc = [CDXRootViewController new];
//    vc.modalPresentationStyle = UIModalPresentationPageSheet;
//    [self presentViewController:vc animated:YES completion:^{
//    }];
    [self.navigationController pushViewController:vc animated:YES];
}

#define ScreenWidth     [[UIScreen mainScreen]bounds].size.width
#define ScreenHeight    [[UIScreen mainScreen]bounds].size.height
#define kAllMessage @"中国日报新浪微博5月10日表示，当地时间周六，特斯拉CEO埃隆·马斯克(Elon Musk)发布两条推文称，特斯拉将起诉阿拉米达县政府，并表示将总部迁出加州。马斯克在推文中表示，他对加州阿拉米达县卫生部门阻止其美国公司总部汽车制造厂在本周五复工表示不满和愤怒。this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe.......this is the test to shoe......."


- (void)showAlertTextViewWithTitle:(NSString *)title
                           message:(NSString *)message
                           actions:(NSArray<NSDictionary *> *)actions {
    
    
    NSString *allMessage = kAllMessage;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    for (NSDictionary *actionInfo in actions) {
        NSString *title = actionInfo[ASAlertControllerTitleKey];
        UIAlertActionStyle style = [actionInfo[ASAlertControllerStyleKey] integerValue];
        void (^handler)(UIAlertAction *action) = actionInfo[ASAlertControllerHandlerKey];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:style handler:handler];
        [alertController addAction:action];
    }
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, ScreenWidth-20, ScreenHeight)];
    textLabel.frame = CGRectMake(0, 0, 100, 200);
    textLabel.font = [UIFont systemFontOfSize:13];
    textLabel.numberOfLines=0;
    textLabel.textAlignment = NSTextAlignmentLeft;

    textLabel.text = allMessage;
    
//    alertController.message = @"sdfsd";
    
//    [alertController setValue:textLabel forKey:@"attributedTitle"];
//    [alertController.view addSubview:textLabel];
    
    
    OADAlertContentViewController *contentVc = [OADAlertContentViewController new];
    contentVc.content = allMessage;
    [alertController setValue:contentVc forKey:@"contentViewController"];
    
    
    
//    UIViewController *v = [[UIViewController alloc] init];
//    v.view.backgroundColor = [UIColor redColor];
//    [v.view addSubview:textLabel];
////    textLabel.frame = v.view.bounds;
//    [alertController setValue:v forKey:@"contentViewController"];
    
//    UIViewController *contentVc = [alertController valueForKey:@"contentViewController"];
//    [contentVc.view addSubview:textLabel];
    
    UIViewController *rootVc = [UIApplication sharedApplication].delegate.window.rootViewController;
    [rootVc presentViewController:alertController animated:YES completion:^{}];
}

- (void)showAlertViewNew {
    
//    __weak typeof(self) weakSelf = self;
    
//    NSString *phoneNumber = @"1234567890-";
//    [self alertWithTitle:nil message:phoneNumber actions:@[
//        @{ASAlertControllerTitleKey: @"取消",
//        },
//        @{ASAlertControllerTitleKey: @"拨打",
//          ASAlertControllerHandlerKey: ^(UIAlertAction *action){
//                NSString *telString = [NSString stringWithFormat:@"tel://%@",phoneNumber];
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
//            },
//        },
//    ]];
    
//    __weak typeof(self) weakself = self;
//    [self showAlertTextViewWithTitle:@"提示"
//                             message:nil
//                             actions:@[
//        @{ASAlertControllerTitleKey: @"确定"},
////        @{ASAlertControllerTitleKey: @"do it",
////          ASAlertControllerHandlerKey: ^(UIAlertAction *action){
//////                NSLog(@"do it...");
//////                [weakself doIt];
////            },
////        },
//    ]];
    
    [OAAlertManager alertWithTitle:@"提示"
                           message:kAllMessage
                           actions:@[
                               @{ASAlertControllerTitleKey: @"确定"}
                           ]
     ];
    
//    [self alertWithTitle:@"title" message:@"message" actions:@[
//        @{@(AlertControllerTitleKey): @"取消",
//          @(AlertControllerStyleKey): @(UIAlertActionStyleDestructive),
//          @(AlertControllerHandlerKey):^(UIAlertAction *action){}
//        },
//        @{@(AlertControllerTitleKey): @"拨打",
//          @(AlertControllerStyleKey): @(UIAlertActionStyleDefault),
//          @(AlertControllerHandlerKey): ^(UIAlertAction *action){
//            NSString *telString = [NSString stringWithFormat:@"tel://%@",@"1111"];
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
//            }
//        },
//    ]];
}

//- (void)alertWithTitle:(NSString *)title message:(NSString *)message actions:(NSArray<NSDictionary *> *)actions{
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
//                                                                             message:message
//                                                                      preferredStyle:UIAlertControllerStyleAlert];
//
//    for (NSDictionary *actionInfo in actions) {
//        NSString *title = actionInfo[@(AlertControllerTitleKey)];
//        UIAlertActionStyle style = [actionInfo[@(AlertControllerStyleKey)] integerValue];
//        void (^handler)(UIAlertAction *action) = actionInfo[@(AlertControllerHandlerKey)];
//
//        UIAlertAction *action = [UIAlertAction actionWithTitle:title style:style handler:handler];
//        [alertController addAction:action];
//    }
//
//    [self presentViewController:alertController animated:YES completion:^{}];
//}




- (void)showAlertViewOld {
//    UIAlertView *telAlertView = [[UIAlertView alloc] initWithTitle:@"上传失败图片"
//                                                           message:nil
//                                                          delegate:self
//                                                 cancelButtonTitle:nil
//                                                 otherButtonTitles:@"拨打", nil];
//    
//    
//    telAlertView.tag=1234;
//    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, ScreenWidth-20, ScreenHeight)];
//    textLabel.font = [UIFont systemFontOfSize:13];
//    textLabel.numberOfLines=0;
//    textLabel.textAlignment = NSTextAlignmentLeft;
//
//    textLabel.text = kAllMessage;
//    [telAlertView setValue:textLabel forKey:@"accessoryView"];
//    
//    [telAlertView show];
}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (buttonIndex == 1) {
//        NSString *telString = [NSString stringWithFormat:@"tel://%@",@"1111"];
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
//    }
//}

//-(void)customAlertTitle:(NSString*)title message:(NSString*)message{
//UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
//UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
//
//UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 270, 50)];
//titleLabel.text = title;
//titleLabel.font = [UIFont boldSystemFontOfSize:20];
//titleLabel.numberOfLines = 2;
//titleLabel.textColor = [UIColor redColor];
//titleLabel.textAlignment = NSTextAlignmentCenter;
//
//[subView addSubview:titleLabel];
//
//UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 270, 50)];
//messageLabel.text = message;
//messageLabel.font = [UIFont systemFontOfSize:18];
//messageLabel.numberOfLines = 2;
//messageLabel.textColor = [UIColor redColor];
//messageLabel.textAlignment = NSTextAlignmentCenter;
//
//[subView addSubview:messageLabel];
//
//[alertView setValue:subView forKey:@"accessoryView"];
//[alertView show];
//}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Class cellClass = [self cellClassAtIndexPath:indexPath];
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [self titleAtIndexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    UIViewController *vc = [self targetViewControllerAtIndexPath:indexPath];
//    [self.navigationController pushViewController:vc animated:YES];

    [self showAlertView];
}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [UIView new];
        tableView.estimatedRowHeight = 0;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
